#include <string.h>
#include <osso-ic.h>
#include <stdio.h>
#include "icd_log.h"
#include "policy_api.h"

static void policy_new_request(struct icd_policy_request *new_request,const GSList *existing_requests,icd_policy_request_new_cb_fn policy_done_cb,gpointer policy_token, gpointer *private)
{
	FILE *f = fopen("/var/log/icdpolicy.log","at");
	fprintf(f,"new_request network_type %s network_id %s\n",new_request->network_type,new_request->network_id);
	fclose(f);
	policy_done_cb(ICD_POLICY_ACCEPTED,new_request,policy_token);
}

static void policy_cancel_request(struct icd_policy_request *request,gpointer *private)
{
	FILE *f = fopen("/var/log/icdpolicy.log","at");
	fprintf(f,"cancel_request network_type %s network_id %s\n",request->network_type,request->network_id);
	fclose(f);
}

static enum icd_policy_status policy_connect(struct icd_policy_request *network,GSList *existing_connections,gpointer *private)
{
	FILE *f = fopen("/var/log/icdpolicy.log","at");
	fprintf(f,"connect network_type %s network_id %s\n",network->network_type,network->network_id);
	fclose(f);
	return ICD_POLICY_ACCEPTED;
}

static enum icd_policy_status policy_restart(struct icd_policy_request *network,guint restart_count,gpointer *private)
{
	FILE *f = fopen("/var/log/icdpolicy.log","at");
	fprintf(f,"connect network_type %s network_id %s\n",network->network_type,network->network_id);
	fclose(f);
	return ICD_POLICY_ACCEPTED;
}

static enum icd_policy_status policy_disconnect(struct icd_policy_request *network,gint reference_count,GSList *existing_connections,gpointer *private)
{
	FILE *f = fopen("/var/log/icdpolicy.log","at");
	fprintf(f,"connect network_type %s network_id %s\n",network->network_type,network->network_id);
	fclose(f);
	return ICD_POLICY_ACCEPTED;
}

void icd_policy_init(struct icd_policy_api *policy_api,icd_policy_nw_add_fn add_network,icd_policy_request_merge_fn merge_requests,icd_policy_request_make_new_fn make_request,icd_policy_scan_start_fn scan_start,icd_policy_scan_stop_fn scan_stop,icd_policy_nw_close_fn nw_close,icd_policy_network_priority_fn priority,icd_policy_service_module_check_fn srv_check)
{
	unlink("/var/log/icdpolicy.log");
	policy_api->disconnect = policy_disconnect;
	policy_api->restart = policy_restart;
	policy_api->connect = policy_connect;
	policy_api->new_request = policy_new_request;
	policy_api->cancel_request = policy_cancel_request;
}
